/**************************************************************************/
/*  Copyright (C) School of Software Engineering of USTC, 2013             
/*                                                                        
/*  FILE NAME: Filter.c                                                  
/*  PRINCIPAL AUTHOR: SA12226340(Song Zeyu)                                                                                                          
/*  LANGUAGE: C++/C                                                          
/*  TARGET ENVIRONMENT: Linux                                             
/*  DATE OF FIRST RELEASE: 2013-05-13                                    
/*  DESCRIPTION:Realization mail spam filtering               
/**************************************************************************/

#include "FolderOpt.h"
#include "read_shmem_test.h"
#include "WordSegFolder.h"
#include "common.h"
#include "MyMap.h"
#include "ProbC.h"
#define MaxMap 60
#define MAXWORDS 15               /*计算概率最大的15个单词的联合概率*/
typedef pair<string,int> pair_map;

int main()
{
	int mailmapsize = 0;
	int spammapsize = 0;
	int pammapsize = 0;
	map<string,int> mapword[MaxMap];                //initialization ten mapword for every file in folder
	map<string,int> spam_map;                         //store the spam mail words
	map<string,int> pam_map;                          //store the legitimate mail
	map<string,int> mailmap;
	vector<pair_map> tVector;
	
	double *wordsprob = NULL;  /*存放p(s|w)概率最大的15单词的概率*/
	int wordslen = 0;
	double spammailprob = 0;               /*垃圾邮件的概率*/
	double threshold = 0.9;     /*阈值设定为0.9，如果spammailprob >threshold，则认为是垃圾邮件，否则为合法邮件*/
	int mailresult;           /*0不通过，1通过*/
	
	mailinfo_t *mailinfo;  
	int mailcount = 1;
	mailinfo = (mailinfo_t *)malloc(sizeof(mailinfo_t));
	wordsprob = (double*)calloc(MAXWORDS,sizeof(double));/*动态创建数组*/
	
	Init_Folder_Map(spam_map,pam_map,mapword);
	spammapsize = MapWordSizeStr(spam_map);
	pammapsize = MapWordSizeStr(pam_map);
	printf("spammapsize = %d\n",spammapsize);
	printf("pammapsize = %d\n",pammapsize);
	
	while(1)   /*一直读邮件*/
	{
		mailinfo = read_shmem();
		printf("mailinfo->mailheadptr->sorip is %u\n",mailinfo->mailheadptr->sorip);
   		printf("mailinfo->mailheadptr->desip is %u\n",mailinfo->mailheadptr->desip);
   		printf("mailinfo->mailheadptr->sorport is %u\n",mailinfo->mailheadptr->sorport);
   		printf("mailinfo->mailheadptr->destport is %u\n",mailinfo->mailheadptr->desport);
   		printf("mailsub is %s\n",mailinfo->mailsubptr);
   		printf("mailtext is %s\n",mailinfo->mailtextptr);
   		printf("mailinfo->mailheadptr->mailsize is %u\n",mailinfo->mailheadptr->mailsize );
    	printf("mailinfo->mailheadptr->mailres is %u\n", mailinfo->mailheadptr->mailres);
    	printf("the %d mailtext is OK\n",mailcount++);
    	
    	SegmentSentenceStrMM(mailinfo->mailtextptr,mailmap);
    	PrintfResult_map(mailmap);
    	mailmapsize = MapWordSizeStr(mailmap);
    	printf("mailmapsize = %d\n",mailmapsize);
    	sortMapByValue(mailmap,tVector);
    	
    	#if 0
    	printf("tvector start\n");
		for(int i=tVector.size()-1;i>=0;i--)  
		{  
			cout<<tVector[i].first<<":"<<tVector[i].second<<endl;  
		}
		printf("tvector end\n"); 
		#endif

		wordsprob = WordsMaxProb(mailmap,spam_map,pam_map,wordslen);	/*求出概率最大的15个单词的频率的数组*/
		spammailprob = WordsJointProb(wordsprob,wordslen);  /*求联合概率*/
		printf("spammailprob = %f\n",spammailprob);
		if(spammailprob>=threshold)
		{
			printf("The mail is spam!\n");
			mailresult = 0;
			
		}
		else
		{
			printf("The mail is pam!\n");
			mailresult = 1;
		}
			
		
	}
	
	free(mailinfo);
	

	return 0;
}
